import 'package:leancloud_rest_api/leancloud_rest_api.dart';
import 'package:test/test.dart';

import 'utils.dart';

void main() {
  group('query', () {
    final bookApi = LraRestApi<BookModal>(dbConfig, 'Book', BookModal.fromJson);
    LraLogger.setLevel(LraLogger.debugLevel, bookApi);

    test('base query', () async {
      int limit = 2;
      var query = LraQuery(limit: limit);
      var list = await bookApi.find(query);
      LraLogger.debug(list);
      assert(list!.isNotEmpty);
      assert(list!.length == limit);

      for (var book in list!) {
        assert(book.objectId != null);
        assert(book.createdAt != null);
        assert(book.updatedAt != null);
      }
    });

    test('count', () async {
      var query = LraQuery(
        where: {
          'createdAt': {
            '\$lte': DateTime.parse('2023-12-15 00:10:50'),
          },
        },
      );
      final count = await bookApi.count(query);
      LraLogger.debug(count);
      assert(count > 0);
    });

    test('order by', () async {
      var query = LraQuery(orderByList: ['createdAt']);
      var list = await bookApi.find(query);
      assert(list![0].createdAt!.isBefore(list[1].createdAt!));

      query = LraQuery(orderByList: ['-createdAt']);
      list = await bookApi.find(query);
      assert(list![0].createdAt!.isAfter(list[1].createdAt!));
    });

    test('include', () async {
      var book =
          await bookApi.get('657b29089a790104bd6d4b72', includes: ['user']);
      assert(book!.user!.username == 'hello');
    });

    test('get', () async {
      var book = await bookApi.get('657b29089a790104bd6d4b72');
      assert(book!.title == '人类如何走到今天');
    });

    test('first', () async {
      var book = await bookApi.first();
      assert(book!.objectId != null);
    });
  });
}
